package xyz.dbqf.smartdict.ui.fragment;

import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import wordroid.business.OperationOfBooks;
import wordroid.database.DataAccess;
import wordroid.database.SqlHelper;
import xyz.dbqf.smartdict.R;
import xyz.dbqf.smartdict.adapter.TabPagerItem;
import xyz.dbqf.smartdict.adapter.ViewPagerAdapter;
import xyz.dbqf.smartdict.ui.fragment.MainFragment;

public class ViewPagerFragment extends Fragment {

    public  static String SETTING_BOOKID="bookID";
    public  static String BOOKNAME = "BOOKNAME";

	private List<TabPagerItem> mTabs = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createTabPagerItem();


        OperationOfBooks OOB = new OperationOfBooks();
        SharedPreferences setting = this.getActivity().getSharedPreferences("wordroid.model_preferences", this.getActivity().MODE_PRIVATE);
        OOB.setNotify(setting.getString("time", "18:00 下午"),ViewPagerFragment.this.getActivity());
        File dir = new File("data/data/xyz.dbqf.smartdict/databases");

        if (!dir.exists())
            dir.mkdir();
        if (!(new File(SqlHelper.DB_NAME)).exists()) {


            FileOutputStream fos;
            try {
                fos = new FileOutputStream(SqlHelper.DB_NAME);

                byte[] buffer = new byte[8192];
                int count = 0;
                InputStream is = getResources().openRawResource(
                        R.raw.wordorid);
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }

                fos.close();
                is.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        SharedPreferences settings=ViewPagerFragment.this.getActivity().getSharedPreferences(SETTING_BOOKID, 0);
        DataAccess.bookID=settings.getString(BOOKNAME, "");
        OOB.UpdateListInfo(ViewPagerFragment.this.getActivity());
    }

    private void createTabPagerItem(){
        mTabs.add(new TabPagerItem(getString(R.string.starred), MainFragment.newInstance(getString(R.string.starred))));
        mTabs.add(new TabPagerItem(getString(R.string.important), XuanzeCikuFragment.newInstance(getString(R.string.important))));
        mTabs.add(new TabPagerItem(getString(R.string.documents), TestFragment.newInstance(getString(R.string.documents))));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_viewpager, container, false);
        rootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ));
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    	ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
    	
    	mViewPager.setOffscreenPageLimit(mTabs.size());
        mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager(), mTabs));
        TabLayout mSlidingTabLayout = (TabLayout) view.findViewById(R.id.tabLayout);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mSlidingTabLayout.setElevation(15);
        }
        mSlidingTabLayout.setupWithViewPager(mViewPager);
    }
}